package com.softwinner.awbt.service;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.RemoteCallbackList;
import android.os.RemoteException;

import com.goodocom.gocsdk.IGocsdkCallback;
import com.softwinner.awbt.common.Commands;
import com.softwinner.awbt.common.GocsdkCommon;
import com.softwinner.awbt.utils.LOG;

public class CommandParser extends GocsdkCommon {
    private static final String TAG = CommandParser.class.getName();

    private RemoteCallbackList<IGocsdkCallback> mCallbacks;
    private Context mContext;
    private boolean isFromBehind = false;

    public CommandParser(RemoteCallbackList<IGocsdkCallback> callbacks, GocsdkService
            gocsdkService) {
        this.mCallbacks = callbacks;
        mContext = gocsdkService;
    }

    private byte[] serialBuffer = new byte[1024];
    private int count = 0;

    private void onSerialCommand(String cmd) {
        LOG.d("接收到的命令" + cmd);

        if (GocsdkService.isBehind) {
            LOG.d("从后台" + cmd);
            if (cmd.startsWith(Commands.IND_INCOMING)) {// 来电
                isFromBehind = true;
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.goodocom.gocsdkfinal", "com.goodocom"
                        + ".gocsdkfinal.activity.TransparentActivity"));
                intent.putExtra("isInComing", true);
                intent.putExtra("inComingNumber", cmd.substring(2));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent
                        .FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                mContext.startActivity(intent);
                setCallMode(true);
            } else if (cmd.startsWith(Commands.IND_OUTGOING_TALKING_NUMBER)) {// 拨出
                isFromBehind = true;
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.goodocom.gocsdkfinal", "com.goodocom"
                        + ".gocsdkfinal.activity.TransparentActivity"));
                intent.putExtra("isInComing", false);
                intent.putExtra("callOutNumber", cmd.substring(2));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent
                        .FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                mContext.startActivity(intent);
                setCallMode(true);
            }
        } else {
            LOG.d("从前台" + cmd);
            int i = mCallbacks.beginBroadcast();
            LOG.d("------------beginBroadcast=" + i);
            while (i > 0) {
                i--;
                IGocsdkCallback cbk = mCallbacks.getBroadcastItem(i);
                try {
                    if (cmd.startsWith(Commands.IND_HFP_CONNECTED)) {
                        cbk.onHfpConnected();
                    } else if (cmd.startsWith(Commands.IND_HFP_DISCONNECTED)) {// HFP已断开:::IA
                        cbk.onHfpDisconnected();
                    } else if (cmd.startsWith(Commands.IND_CALL_SUCCEED)) {//
                        // 去电:::IC[numberlen:2][number]
                        setCallMode(true);
                        if (cmd.length() < 4) {
                            cbk.onCallSucceed("");
                        } else {
                            cbk.onCallSucceed(cmd.substring(4));
                        }
                    } else if (cmd.startsWith(Commands.IND_INCOMING)) {//
                        // 来电:::ID[numberlen:2][number]
                        setCallMode(true);
                        if (cmd.length() < 2) {
                            cbk.onIncoming("");
                        } else {
                            cbk.onIncoming(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_HANG_UP)) {//
                        // 挂机:::IF[numberlen:2][number]
                        setCallMode(false);
                        if (isFromBehind) {
                            //TODO ADD BY XSJ
//                            Handler handler = TransparentActivity.getHandler();
//                            if (handler != null) {
//                                handler.sendEmptyMessage(MessageKey.MSG_HANGUP_PHONE);
//                            }
                        } else {
                            cbk.onHangUp();
                        }
                        isFromBehind = false;

                    } else if (cmd.startsWith(Commands.IND_TALKING)) {//
                        // 通话中:::IG[numberlen:2][number]
                        //TODO ADD BY XSJ
                        if (isFromBehind) {
//                            Handler handler = TransparentActivity.getHandler();
//                            if (handler != null) {
//                                handler.sendEmptyMessage(MessageKey.MSG_CONNECTION_PHONE);
//                            }
                        } else {
                            if (cmd.length() < 4) {
                                cbk.onTalking("");
                            } else {
                                cbk.onTalking(cmd.substring(4));
                            }
                        }
                    } else if (cmd.startsWith(Commands.IND_RING_START)) {// 开始响铃
                        cbk.onRingStart();
                    } else if (cmd.startsWith(Commands.IND_RING_STOP)) {// 停止响铃
                        cbk.onRingStop();
                    } else if (cmd.startsWith(Commands.IND_HF_LOCAL)) {//
                        cbk.onHfpLocal();
                    } else if (cmd.startsWith(Commands.IND_HF_REMOTE)) {// 蓝牙接听
                        cbk.onHfpRemote();
                    } else if (cmd.startsWith(Commands.IND_IN_PAIR_MODE)) {// 进入配对模式:::II
                        cbk.onInPairMode();
                    } else if (cmd.startsWith(Commands.IND_EXIT_PAIR_MODE)) {// 退出配对模式
                        cbk.onExitPairMode();
                    } else if (cmd.startsWith(Commands.IND_INIT_SUCCEED)) {// 上电初始化成功:::IS
                        cbk.onInitSucceed();
                    } else if (cmd.startsWith(Commands.IND_MUSIC_PLAYING)) {// 音乐播放
                        cbk.onMusicPlaying();
                    } else if (cmd.startsWith(Commands.IND_MUSIC_STOPPED)) {// 音乐停止
                        cbk.onMusicStopped();
                    } else if (cmd.startsWith(Commands.IND_VOICE_CONNECTED)) {
                        // cbk.onVoiceConnected();
                    } else if (cmd.startsWith(Commands.IND_VOICE_DISCONNECTED)) {
                        // cbk.onVoiceDisconnected();
                    } else if (cmd.startsWith(Commands.IND_AUTO_CONNECT_ACCEPT)) {
                        if (cmd.length() < 4) {
                            // Log.e(TAG, cmd + "=====error command");
                        } else {
                            cbk.onAutoConnectAccept(cmd.charAt(2) != '0', cmd.charAt(3) != '0');
                        }
                    } else if (cmd.startsWith(Commands.IND_CURRENT_ADDR)) {
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd + "==== error command");
                        } else {
                            cbk.onCurrentAddr(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_CURRENT_NAME)) {
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd + "==== error command");
                        } else {
                            cbk.onCurrentName(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_AV_STATUS)) {
                        LOG.d("--xsj->callback music status cmd.length()=" + cmd.length() + " " +
                                "cmd=" + cmd);
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd + "=====error");
                        } else {
                            cbk.onAvStatus(Integer.parseInt(cmd.substring(2, 3)));
                        }
                    } else if (cmd.startsWith(Commands.IND_HFP_STATUS)) {
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd +" ==== error");
                        } else {
                            int status = Integer.parseInt(cmd.substring(2, 3));
                            cbk.onHfpStatus(status);
                        }
                    } else if (cmd.startsWith(Commands.IND_VERSION_DATE)) {
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd + "====error");
                        } else {
                            cbk.onVersionDate(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_CURRENT_DEVICE_NAME)) {
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd + "====error");
                        } else {
                            cbk.onCurrentDeviceName(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_CURRENT_PIN_CODE)) {
                        if (cmd.length() < 3) {
                            // Log.e(TAG, cmd + "====error");
                        } else {
                            cbk.onCurrentPinCode(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_A2DP_CONNECTED)) {
                        cbk.onA2dpConnected();
                    } else if (cmd.startsWith(Commands.IND_A2DP_DISCONNECTED)) {
                        cbk.onA2dpDisconnected();
                    } else if (cmd.startsWith(Commands.IND_CURRENT_AND_PAIR_LIST)) {
                        if (cmd.length() < 15) {
                            // Log.e(TAG, cmd + "====error");
                        } else if (cmd.length() == 15) {
                            cbk.onCurrentAndPairList(Integer.parseInt(cmd.substring(2, 3)), "",
                                    cmd.substring(3, 15));
                        } else {
                            cbk.onCurrentAndPairList(Integer.parseInt(cmd.substring(2, 3)), cmd
                                    .substring(15), cmd.substring(3, 15));
                        }
                    } else if (cmd.startsWith(Commands.IND_PHONE_BOOK)) {// 联系人信息
                        if (cmd.length() < 6) {
                            // Log.e(TAG, cmd + "====error");
                        } else {
                            int nameLen = Integer.parseInt(cmd.substring(2, 4));
                            int numLen = Integer.parseInt(cmd.substring(4, 6));
                            String name;
                            String num;
                            byte[] bytes = cmd.getBytes();
                            if (nameLen > 0) {
                                byte[] buffer = new byte[nameLen];
                                System.arraycopy(bytes, 6, buffer, 0, nameLen);
                                name = new String(buffer);
                            } else {
                                name = "";
                            }
                            if (numLen > 0) {
                                byte[] buffer = new byte[numLen];
                                System.arraycopy(bytes, 6 + nameLen, buffer, 0, numLen);
                                num = new String(buffer);
                            } else {
                                num = "";
                            }
                            cbk.onPhoneBook(name, num);
                        }
                    } /*
                     * else if (cmd.startsWith(Commands.IND_SIM_BOOK)) { if
                     * (cmd.length() < 6) { // Log.e(TAG, cmd + "====error"); }
                     * else { int nameLen = Integer.parseInt(cmd.substring(2,
                     * 4)); int numLen = Integer.parseInt(cmd.substring(4, 6));
                     * String name; String num; byte[] bytes = cmd.getBytes();
                     * if (nameLen > 0) { byte[] buffer = new byte[nameLen];
                     * System.arraycopy(bytes, 6, buffer, 0, nameLen); name =
                     * new String(buffer); } else { name = ""; } if (numLen > 0)
                     * { byte[] buffer = new byte[numLen];
                     * System.arraycopy(bytes, 6 + nameLen, buffer, 0, numLen);
                     * num = new String(buffer); } else { num = ""; }
                     * cbk.onPhoneBook(name, num); } }
                     */ else if (cmd.startsWith(Commands.IND_PHONE_BOOK_DONE)) {// 更新联系人完成
                        cbk.onPhoneBookDone();
                    } else if (cmd.startsWith(Commands.IND_SIM_DONE)) {
                        cbk.onSimDone();
                    } else if (cmd.startsWith(Commands.IND_CALLLOG_DONE)) {
                        cbk.onCalllogDone();
                    } else if (cmd.startsWith(Commands.IND_CALLLOG)) {
                        if (cmd.length() < 4) {
                            // Log.e(TAG, cmd + "====error");
                        } else {
                            String[] split = cmd.substring(3).split("\\[FF\\]");
                            for (String string : split) {
                                LOG.d(string);
                            }
                            cbk.onCalllog(Integer.parseInt(cmd.substring(2, 3)), split[0],
                                    split[1]);
                        }
                    } else if (cmd.startsWith(Commands.IND_DISCOVERY)) {
                        if (cmd.length() < 14) {
                            // Log.e(TAG, cmd+"===error");
                        } else if (cmd.length() == 14) {
                            cbk.onDiscovery("", cmd.substring(2));
                        } else {
                            cbk.onDiscovery(cmd.substring(14), cmd.substring(2, 14));
                        }
                    } else if (cmd.startsWith(Commands.IND_DISCOVERY_DONE)) {
                        cbk.onDiscoveryDone();
                    } else if (cmd.startsWith(Commands.IND_LOCAL_ADDRESS)) {
                        if (cmd.length() != 14) {
                        }
                        cbk.onLocalAddress(cmd.substring(2));
                    } else if (cmd.startsWith(Commands.IND_OUTGOING_TALKING_NUMBER)) {
                        if (cmd.length() <= 2) {
                            cbk.onOutGoingOrTalkingNumber("");
                        } else {
                            cbk.onOutGoingOrTalkingNumber(cmd.substring(2));
                        }
                    } else if (cmd.startsWith(Commands.IND_MUSIC_INFO_STRING)) {
                        if (cmd.length() <= 2) {
                            // Log.e(TAG, cmd+"===error");
                        } else {
                            String info = cmd.substring(2);
                            String[] arr = info.split("\\[FF\\]");
                            if (arr.length != 5) {
                                // Log.e(TAG, cmd+"===error");
                            } else {
                                cbk.onMusicInfo(arr[0], arr[1], Integer.parseInt(arr[2]), Integer
                                        .parseInt(arr[3]), Integer.parseInt(arr[4]));
                            }
                        }
                    } else if (cmd.startsWith(Commands.IND_PROFILE_ENABLED)) {
                        if (cmd.length() < 12) {
                            // Log.e(TAG, cmd + "====error");
                        } else {
                            boolean[] enabled = new boolean[10];
                            for (int ii = 0; ii < 10; ii++) {
                                if (cmd.charAt(ii + 2) == '0') {
                                    enabled[ii] = false;
                                } else {
                                    enabled[ii] = true;
                                }
                            }
                            cbk.onProfileEnbled(enabled);
                        }
                    } else if (cmd.startsWith(Commands.IND_MESSAGE_LIST)) {
                        String text = cmd.substring(2);
                        if (text.length() == 0) {
                            // Log.e("goc", "cmd error:param==0"+cmd);
                        } else {
                            String[] arr = text.split("\\[FF\\]", -1);
                            if (arr.length != 6) {
                                // Log.e("goc",
                                // "cmd error:arr.length="+arr.length+";"+cmd);
                            } else {
                                cbk.onMessageInfo(arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]);
                            }
                        }
                    } else if (cmd.startsWith(Commands.IND_MESSAGE_TEXT)) {
                        cbk.onMessageContent(cmd.substring(2));
                    } else if (cmd.startsWith(Commands.IND_OK)) {
                        // TODO:OK ind
                    } else if (cmd.startsWith(Commands.IND_ERROR)) {

                    } else {

                    }
                } catch (RemoteException e) {

                }
            }

            mCallbacks.finishBroadcast();
        }
    }

    private void onByte(byte b) {
        if ('\n' == b) return;
        if (count >= 1000) count = 0;
        if ('\r' == b) {
            if (count > 0) {
                byte[] buf = new byte[count];
                System.arraycopy(serialBuffer, 0, buf, 0, count);
                onSerialCommand(new String(buf));
                count = 0;
            }
            return;
        }
        if ((b & 0xFF) == 0xFF) {
            serialBuffer[count++] = '[';
            serialBuffer[count++] = 'F';
            serialBuffer[count++] = 'F';
            serialBuffer[count++] = ']';
        } else {
            serialBuffer[count++] = b;
        }
    }

    public void onBytes(byte[] data) {
        for (byte b : data) {
            onByte(b);
        }
    }

    private void setCallMode(boolean isCall) {
        AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        if (isCall) {
            audioManager.setMode(AudioManager.MODE_IN_CALL);
        } else {
            audioManager.setMode(AudioManager.MODE_NORMAL);
        }
    }
}
